// requires jQuery
// requires wapp.js
// optional status divs
// #cache_update_status - current status of update
// #update_link   		- manual update link


function updateCache(){
	//cache.swapCache();
	wapp.log('Loading Manifest: ' + $('html').attr('manifest'));
	wapp.log('updateCache() called');
	cache.update();
}
function ask_restart() {
    if (confirm("App Updated. " + cache_cnt + " files downloaded. Restart app?")){
    	restart();
    }
    return false;
}
function restart(){
	window.location.reload();
}
function updateStarted(){
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'Checking<br/>';
	if (document.getElementById('update_link')) {
		document.getElementById('update_link').innerHTML = "Checking...";
		document.getElementById('update_link').onclick = function(){return false;};
	}	
}
function updateDownloading(){
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'Updating<br/>';
	if (document.getElementById('update_link')) {
		document.getElementById('update_link').onclick = function(){return false;};
		document.getElementById('update_link').innerHTML = "Updating..." + cache_cnt_tmp;
	}	
}
function updateDone(firstRun){
	if (cache_updated){
		cache_cnt = cache_cnt_tmp;
		if (window.localStorage){
			localStorage.wapp_cache_last_cnt = cache_cnt;
		}		
		//wapp.setCookie('manifest','',1);
		wapp.log('manifest_id ' + wapp.getCookie('manifest_id'));
		if (document.getElementById('update_link')) {
			if (ask_onclick) {
				document.getElementById('update_link').onclick = ask_restart;
			} else {
				document.getElementById('update_link').onclick = restart;
			}
			if (firstRun) {
				document.getElementById('update_link').innerHTML = cache_cnt + " files downloaded.";
			} else {
				document.getElementById('update_link').innerHTML = cache_cnt + " files downloaded. Restart";
			}
		}	
		if (auto_ask_to_restart)
			ask_restart();		
	} else {
		if (document.getElementById('update_link')) {
			document.getElementById('update_link').onclick = updateCache;
			document.getElementById('update_link').innerHTML = "Check for update";
		}		
	}
}
function cache_updateready(){
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'Update Ready<br/>';	
	cache_updated = true;
	cache_first_load = false;
	updateDone(false);
	return false;
}
function cache_noupdate(){
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'No Update<br/>';	
	if (document.getElementById('update_link')) {
		document.getElementById('update_link').onclick = updateCache;
		document.getElementById('update_link').innerHTML = "Check for update";
	}		
	updateDone(false);
}
function cache_progress(){
	cache_cnt_tmp ++;
	if (document.getElementById('update_link')) {
		document.getElementById('update_link').innerHTML = cache_cnt_tmp + " files downloaded.";
	}
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'Progress ' + cache_cnt_tmp + '<br />';
}
function cache_downloading(){
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'Downloading<br/>';
	updateDownloading();
}
function cache_checking(){
	cache_cnt_tmp = 0;
	updateStarted();
}
function cache_cached(){
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'Cached<br/>';
	cache_updated = true;
	updateDone(true);
}
function cache_error(){
	if (document.getElementById('cache_update_status'))
		document.getElementById('cache_update_status').innerHTML += 'Error<br/>';
	updateDone(false);
}

var statmsg = new Array();
statmsg[0] = 'UNCACHED';
statmsg[1] = 'IDLE';
statmsg[2] = 'CHECKING';
statmsg[3] = 'DOWNLOADING';
statmsg[4] = 'UPDATEREADY';
statmsg[5] = 'OBSOLETE';

var auto_ask_to_restart = false;
var ask_onclick = true;
var cache_updated = false;
var cache = window.applicationCache;
var cache_cnt = 0;
var cache_cnt_tmp = 0;

$().ready(function(){
//	if (window.localStorage){
//		if (localStorage.wapp_cache_last_cnt) {
//			cache_cnt = localStorage.wapp_cache_last_cnt;
//			wapp.log('File last cached: ' + cache_cnt);
//		}
//	}	
//	cache.addEventListener('updateready', cache_updateready, false);
//	cache.addEventListener('noupdate', cache_noupdate, false);
//	cache.addEventListener('progress', cache_progress, false);
//	cache.addEventListener('downloading', cache_downloading, false);
//	cache.addEventListener('checking', cache_checking, false);
//	cache.addEventListener('cached', cache_cached, false);
//	cache.addEventListener('error', cache_error, false);
});